package cn.wolfcode.service.impl;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.mapper.MessageBoardMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageBoardServiceImpl implements IMessageBoardService {
    @Autowired
    private MessageBoardMapper messageBoardMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        messageBoardMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void insert(MessageBoard messageBoard) {
        messageBoard.setCreateTime(new Date());
        messageBoard.setReplystatus(false);
        messageBoardMapper.insert(messageBoard);
    }

    @Override
    public MessageBoard get(Long id) {
        return messageBoardMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<MessageBoard> listAll() {
        return messageBoardMapper.selectAll();
    }

    @Override
    @Transactional
    public void update(MessageBoard messageBoard) {
        messageBoardMapper.updateByPrimaryKey(messageBoard);
    }

    @Override
    public PageInfo<MessageBoard> query(QueryObject qo) {
        // 使用分页插件，传入当前页，每页显示数量
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),"mb.create_time desc");
        // 返回查询一页展示的数据
        return new PageInfo<>(messageBoardMapper.selectForList(qo));
    }

}
